/************************************************************************************
* CollectorSortFilter.cpp                                                                        *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "CollectorSortFilter.h"
#include "Collector.h"


CollectorSortFilter::CollectorSortFilter(QObject* parent)
    : QSortFilterProxyModel(parent) {
    m_regExp = new QRegExp(QString(""));
    m_regExp->setCaseSensitivity(Qt::CaseInsensitive);
    m_criteria = FC_All;
    m_rule = OrRule;
}

CollectorSortFilter::~CollectorSortFilter() {
    if(m_regExp) {
        delete m_regExp;
    }
}

void CollectorSortFilter::setExpression(QString expression) {
    m_regExp->setPattern(expression);
    invalidateFilter();
}

void CollectorSortFilter::setFilterCriteria(int searchType) {
    m_criteria = searchType;
    invalidateFilter();
}


void CollectorSortFilter::setFilterRule(FilterRule rule) {
    m_rule = rule;
    invalidate();
}

bool CollectorSortFilter::filterAcceptsRow(
    int source_row, const QModelIndex &source_parent) const {
    Collector* model =  (Collector*) sourceModel();
    if(source_row < 0
            || source_row >= model->numItems()
            || ! sourceModel()->index(source_row, 0).isValid()) {
        return false;
    }
    QString uuid = sourceModel()->data(sourceModel()->index(source_row, 0)).toString();
    CollectorItem* item = model->item(uuid);

    bool accept = (m_rule == AndRule);

    if (m_criteria == 0x00 || (m_rule == OrRule && m_criteria == 0x0F)) {
        accept = item->title().contains(*m_regExp) ||
                 item->artist().contains(*m_regExp)||
                 item->album().contains(*m_regExp);
    }
    else if(m_criteria == 0x0F) {
        accept = item->title().contains(*m_regExp) &&
                 item->artist().contains(*m_regExp)&&
                 item->album().contains(*m_regExp);
    }
    else {
        if(m_criteria & FC_Title) {
            accept = applyRule(accept, item->title().contains(*m_regExp));
        }
        if(m_criteria & FC_Artist) {
            accept = applyRule(accept, item->artist().contains(*m_regExp));
        }
        if(m_criteria & FC_Album) {
            accept = applyRule(accept, item->album().contains(*m_regExp));
        }
    }
    return accept;
}


bool CollectorSortFilter::applyRule(bool first, bool second) const {
    return (m_rule == AndRule)?(first && second) : (first || second);
}

bool CollectorSortFilter::lessThan(
    const QModelIndex &left, const QModelIndex &right) const {
    QVariant lData = left.data();
    QVariant rData = right.data();
    return QString::localeAwareCompare(lData.toString(), rData.toString()) < 0;
}
